package czk.concrent.juc.countDOwnLatch.practice;

import java.util.Arrays;
import java.util.concurrent.*;

/**
 * @Author:ChenZhangKun
 * @Date: 2021/7/21 22:12
 */
public class PeaceFilePractice {
    public static void main(String[] args) throws InterruptedException {
        // 创建线程池
        ExecutorService threadPool = Executors.newFixedThreadPool(5);
        // 创建线程计数器
        CountDownLatch countDownLatch = new CountDownLatch(5);
        // 创建数组
        String[] person = new String[5];
        // 循环
        for (int i = 0; i < 5; i++) {
            // lambda的asm实现，对外部类的不可见性
            int k = i;
            threadPool.submit(() -> {
                // 模拟加载到100%
                for (int j = 0; j <= 100; j++) {
                    person[k] = j + "%";
                    // 模拟卡顿
                    try {
                        TimeUnit.MILLISECONDS.sleep(ThreadLocalRandom.current().nextLong(100L));
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    // 输出当前
                    System.out.print("\r" + Arrays.toString(person));
                }
                // 线程计数器减一
                countDownLatch.countDown();
            });
        }
        // 线程阻塞
        System.out.println("游戏加载中");
        countDownLatch.await();
        System.out.println();
        System.out.println("游戏加载完成");
        threadPool.shutdown();
    }
}
